package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.*;
import com.project.mapper.*;
import com.project.service.ICusReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CusReturnServiceImp implements ICusReturnService {
    @Autowired
    private ICusReturnMapper cusReturnMapper;
    @Autowired
    private IOrderMapper orderMapper;
    @Autowired
    private IOrderInfoMapper orderInfoMapper;
    @Autowired
    private IOrderReturnProductMapper orderReturnProductMapper;
    @Autowired
    private IInventoryInfoMapper inventoryInfoMapper;

    @Override
    public String getOrder(String orderNum) {
        //根据订单编号查询到该笔订单
        OrderBean orderBean = orderMapper.selectById(orderNum);
        //根据订单编号查询到订单详情的集合
        QueryWrapper<OrderInfoBean> qw  = new QueryWrapper<>();
        qw.eq("fk_order",orderNum);
        List<OrderInfoBean> infoBeans = orderInfoMapper.selectList(qw);
        //客户退货订单添加--退货订单编号（输入）、订单编号（输入）、退款金额（提取）、退款时间（输入）、审核（暂输入）、客户名字（提取）
        CusReturnBean cusReturnBean = new CusReturnBean();
        cusReturnBean.setReturnOrder(orderNum+"Test");
        cusReturnBean.setOrderNum(orderNum);
        cusReturnBean.setTotalPrice(orderBean.getTotalPrice());
        cusReturnBean.setReturnDate(LocalDate.now());
        cusReturnBean.setAuditStatus("test");
        cusReturnBean.setCustomerName(orderBean.getCustomerName());
        cusReturnMapper.insert(cusReturnBean);
        //遍历订单详情集合
        List<OrderReturnProductBean> orderReturnProductBeans= new ArrayList<>();
        for (int i = 0;i<infoBeans.size();i++){
            //客户订单详情（订单退货商品）--客户退货外键（输入）、商品明细编号（输入）、商品名、商品数量、成交价、颜色、类型
            OrderReturnProductBean orderReturnProductBean = new OrderReturnProductBean();
            orderReturnProductBean.setFk_cusReturnId(String.valueOf(cusReturnBean.getCusReturnId()));
            orderReturnProductBean.setProductId(infoBeans.get(i).getCommodityId());
            orderReturnProductBean.setProductName(infoBeans.get(i).getCommodityName());
            orderReturnProductBean.setProductNum(infoBeans.get(i).getCommodityNum());
            orderReturnProductBean.setOutPrice(infoBeans.get(i).getCommodityPrice());
            orderReturnProductBean.setColor(inventoryInfoMapper.selectById(infoBeans.get(i).getCommodityId()).getColor());
            orderReturnProductBean.setProductType(infoBeans.get(i).getType());
            //添加
            orderReturnProductMapper.insert(orderReturnProductBean);
            orderReturnProductBeans.add(orderReturnProductBean);
        }
        cusReturnBean.setOrderReturnProductBeans(orderReturnProductBeans);
        cusReturnMapper.updateById(cusReturnBean);

        return "ok";
    }

    @Override
    public String cusReturn(String returnOrder) {
        QueryWrapper<CusReturnBean> qw = new QueryWrapper<CusReturnBean>();
        qw.eq("returnOrder",returnOrder);
        CusReturnBean cusReturnBean = cusReturnMapper.selectOne(qw);
        if (cusReturnBean != null){
            QueryWrapper<OrderReturnProductBean> qwor = new QueryWrapper<>();
            qwor.eq("fk_cusReturnId",cusReturnBean.getCusReturnId());
            List<OrderReturnProductBean> orderReturnProductBeans = new ArrayList<>();
            for (int i = 0;i<orderReturnProductBeans.size();i++){
                Integer returnProudctNum = orderReturnProductBeans.get(i).getProductNum();
                InventoryInfoBean inventoryInfoBean = inventoryInfoMapper.selectById( orderReturnProductBeans.get(i).getProductId());
                inventoryInfoBean.setProductInfoNum(returnProudctNum+inventoryInfoBean.getProductInfoNum());
                inventoryInfoMapper.updateById(inventoryInfoBean);
            }
            cusReturnBean.setOrderReturnProductBeans(orderReturnProductBeans);
            return "ok";
        }
        return null;
    }
}
